package com.ocean.service.edu.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ocean.dao.edu.CourseDao;
import com.ocean.dao.edu.GCourseDao;
import com.ocean.dao.edu.TeacherDao;
import com.ocean.dto.GCourseListDto;
import com.ocean.entity.edu.Course;
import com.ocean.entity.edu.GCourse;
import com.ocean.entity.edu.Teacher;
import com.ocean.service.edu.GCourseService;
import com.ocean.po.GCoursePo;
import com.ocean.vo.R;
import com.ocean.dto.GCourseAddDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author xk
 * @version 1.0
 */
@Service
public class GCourseServiceImpl implements GCourseService {
    @Autowired
    private TeacherDao teacherDao;

    @Autowired
    private CourseDao courseDao;

    @Autowired
    private GCourseDao dao;

    @Override
    public R save(GCourseAddDto teacherVO) {
        Teacher teacher = teacherDao.selectByName(teacherVO.getTname());
        Course course = courseDao.selectByName(teacherVO.getCname());
        if (teacher != null && course != null) {
            if (dao.insert(new GCourse(teacher.getId(), course.getId())) > 0) {
                return R.ok();
            } else {
                return R.fail();
            }
        } else {
            return R.fail();
        }
    }

    @Override
    public R delete(int id) {
        if (dao.delete(id) > 0) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    @Override
    public R update(GCourseAddDto teacherVO) {
        Teacher teacher = teacherDao.selectByName(teacherVO.getTname());
        Course course = courseDao.selectByName(teacherVO.getCname());
        if (teacher != null && course != null) {
            if (dao.update(new GCourse(teacherVO.getId(), teacher.getId(), course.getId())) > 0) {
                return R.ok();
            } else {
                return R.fail();
            }
        } else {
            return R.fail();
        }
    }

    @Override
    public R queryPage(int page, int size) {
        PageHelper.startPage(page, size);
        PageInfo<GCoursePo> pageInfo = new PageInfo<>(dao.selectAll());
        return R.ok(pageInfo);
    }

    @Override
    public R queryPageWhere(GCourseListDto dto) {
        PageHelper.startPage(dto.getPage(), dto.getSize());
        PageInfo<GCoursePo> pageInfo=new PageInfo<>(dao.selectWhere(dto.getTname(),dto.getCname()));
        return R.ok(pageInfo);

    }
}
